package com.broad.school_description.service.impl;

import com.broad.school_description.service.CountNumService;
import com.broad.school_description.utils.CountNumUtils;
import com.fasterxml.jackson.databind.annotation.JsonAppend;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class CountPageNumServiceImpl implements CountNumService {
    @Autowired
    RedisTemplate<String,String> redisTemplate;
    @Override
    public void uriFound(String uri) {
        try {
            incrementRedisPageCount(redisTemplate,uri);
        }catch (Exception e){
            System.out.println(e.getMessage());
        }

    }

    private void incrementRedisPageCount(RedisTemplate<String,String> redisTemplate,String uri){
        String indexCount = redisTemplate.opsForValue().get(uri);
        if (indexCount == null || Integer.parseInt(indexCount) <= 0){
            redisTemplate.opsForValue().set(uri,"1");
        } else {
            redisTemplate.opsForValue().increment(uri);
        }
    }

}
